<?php

class Baidu_fanyi_api_service extends MY_Service {

    public $_server;
    public $_api_config;

    function __construct() {
        parent::__construct();

        $this->load->model("api_log_model");
        $this->load->library("rsa");
        $this->_api_config = config_item("baidu_fanyi");
    }

    /*
     * 
     */

    public function fanyi($data, $from = "zh", $to = "en") {

//        if ($data["dingdan_shenbao_status"] == 1) {
//            return [
//                "status_code" => 0,
//                "msg" => "订单已经申报",
//            ];
//        }
        //print_r($data);
        //$q = join("\n", $data);
        $q = $data;
        //$q = str_replace("\n", "//", $data);

        $params = [
            "from" => $from,
            "to" => $to,
            "q" => $q,
        ];
        //print_r($params);

        $data = $apiUrl = $this->_api_config["api_url"];

        $result = $this->postApi($params, '百度翻译', $apiUrl);
        return $result;
        //echo '<pre>';
        //print_r($rs);
        //[{"code":"","crossOrderId":"84c886a31fda4368a4afe713a0bcfdcf","msg":"","orderNo":"qytest200210001","status":"success"}]
    }

    /**
     * Post 方式访问接口
     * @param array $params 参数数组
     * @param string $item 类型
     * @param string $url 接口地址
     * @return boolean|array
     */
    public function postApi($params, $item, $url) {

        $param = [
            "appid" => $this->_api_config["appid"],
            "salt" => time(),
            "secret" => $this->_api_config["secret"],
        ];
        $params = array_merge($param, $params);

        //print_r($params);

        $sign_str = $params["appid"] . $params["q"] . $params["salt"] . $params["secret"];
        $sign = md5($sign_str);
        $params["sign"] = $sign;

        $str = "";
        foreach ($params as $k => $v) {
            if ($k == "q") {
                $v = urlencode($v);
            }
            $str .= "&" . $k . "=" . $v;
        }
        $str = trim($str, "&");

        $request_data = [
            "url" => $url,
            "params" => $params,
        ];

        $header = ['Content-Type:application/x-www-form-urlencoded'];

        $url = $url . "?" . $str;

        //echo $url."<br>";


        $rs = $this->post($url, [], $header);

        //增加日志
        $this->api_log_model->addLog(1, $item, $request_data, $rs);

        $rs = jsonDecode($rs);
        $rs = !empty($rs[0]) ? $rs[0] : $rs; //因为有些接口是可以批量上传的，我们选择一次只传一条；

        $status_code = 0;
        $msg = "失败";
        $data = [];

        if (empty($rs)) {
            $msg = "接口请求失败";
        } else {
            if (!empty($rs["error_code"])) {
                if ($rs["error_code"] == 5200) {
                    $status_code = 2; //成功,但是没有结果
                    $msg = "成功(" . $rs["error_code"] . ")";
                } else {
                    $msg = $rs["error_msg"] . "(" . $rs["error_code"] . ")";
                }
            } else {
                if (!empty($rs["trans_result"][0]["dst"])) {
                    $status_code = 1;
                    $msg = "翻译成功";
                    $data = $rs;
                }
            }
        }
        return [
            "status_code" => $status_code,
            "msg" => $msg,
            "data" => $data,
        ];
    }

    /*
     * post表单数据
     */

    public function post($url, $postArray = [], $header = null) {
        $ch = curl_init();

        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray);

        if (strtolower(substr($url, 0, 5)) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }

        $output = curl_exec($ch);

        curl_close($ch);

        return $output;
    }

}
